package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.context.UserThreadLocal;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.ReservationService;
import com.zzyl.nursing.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 新增预约
     *
     * @param reservation
     */
    @Override
    public void insertReservation(Reservation reservation) {
        Long userId = UserThreadLocal.getUserId();
        Integer cancelledCount = reservationMapper.selectCancelledCount(userId);
        if (ObjectUtil.isNotEmpty(cancelledCount)){
            if (cancelledCount >= Reservation.COUNT_CANCEL){
                throw new RuntimeException("取消预约大于三次, 请明日再次预约");
            }
        }

        reservation.setCreateBy(userId);
        reservation.setStatus(Reservation.STATUS_WAIT);
        reservation.setCreateTime(LocalDateTime.now());
        reservationMapper.insert(reservation);
    }

    /**
     * 取消预约
     *
     * @param id
     */
    @Override
    public void cancelReservation(Long id) {
        Reservation reservation = Reservation.builder().id(id).status(Reservation.STATUS_CANCEL).updateBy(UserThreadLocal.getUserId()).updateTime(LocalDateTime.now()).build();
        reservationMapper.updateById(reservation);
    }

    /**
     * 查询取消预约数量
     *
     * @return
     */
    @Override
    public Integer getCancelledCount() {
        Long userId = UserThreadLocal.getUserId();
        Integer count = reservationMapper.selectCancelledCount(userId);
        return count;
    }

    /**
     * 分页查询预约
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public TableDataInfo<Reservation> selectReservationByPage(Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        Long userId = UserThreadLocal.getUserId();
        Page<Reservation> page = reservationMapper.selectReservationByPage(userId,status);
        return new TableDataInfo<Reservation>(page.getResult(), Math.toIntExact(page.getTotal()));
    }

    /**
     * 查询每个时间段剩余的预约此数
     *
     * @param time 根据前端选择的日期进行改变, 起始时间为当天的早上8点
     * @return
     */
    @Override
    public List<ReservationVo> selectCountByTime(Long time) {
        LocalDateTime startTime = DateUtil.date(time).toLocalDateTime();
        LocalDateTime endTime = DateUtil.date(time).toLocalDateTime().plusHours(18);

        List<ReservationVo> reservationList = reservationMapper.selectReservationByTime(startTime, endTime);
        return reservationList;
    }
}
